<?php
    class MainModel
    {
        private $db;
        
        public function __construct()
        {
            $this->db = db::getInstance();
        }
        
        // Get an item
        public function getItem($id_item)
        {
            $result  = $this->db->select('*', 'items', array('where'=>array('id'=>$id_item)));
            $item    = mysql_fetch_assoc($result);
            $options = array();
            foreach($this->getOptions($item['id_category']) as $option) {                
                if($option['type']=='category') {
                    // If the optionType is a category, the value must be preserved, because it's the ID
                    // of the category refering to. The value in this case will be the item selected from that category.
                    $option['id_category'] = $option['value'];
                }
                if($option['hasmany']==0) {
                    $option['value'] = $this->db->get('value', 'values', array('id_option'=>$option['id'], 'id_item'=>$id_item));
                } else {
                    $result = $this->db->select('value', 'values', array('where'=>array('id_option'=>$option['id'], 'id_item'=>$id_item)));
                    $values = array();
                    while($value = mysql_fetch_assoc($result)) {
                        array_push($values, $value['value']);
                    }
                    $option['value'] = $values;
                }
                array_push($options, $option);
            }
            $item['options'] = $options;            
            return $item;
        }
        
        // Create an empty item according to the settings of a category
        public function createEmptyItem($id_category)
        {
            $item        = array('id'=>0, 'name'=>'', 'id_category'=>$id_category);
            $options     = $this->getOptions($id_category);
            $item['options'] = $options;            
            return $item;            
        }
        
        // Return an array with the available options of this category
        public function getOptions($id_category)
        {
            $options     = array();
            $prefix      = $this->db->getPrefix();
            $id_datatype = $this->db->get('id_datatype', 'categories', array('id'=>$id_category));
            $result      = $this->db->mq('SELECT A.*, B.`hasmany` FROM `'.$prefix.'options` A, `'.$prefix.'datatype_option` B WHERE B.`id_datatype`='.$id_datatype.' AND B.`id_option` = A.`id`;');
            while($option = mysql_fetch_assoc($result)) {
                if(empty($option['description'])) {
                    $option['description']=$option['name'];
                }
                array_push($options, $option);
            }
            return $options;
        }
        
        // Add a new item
        public function addItem($item)
        {
            $this->db->insert('items', array('name'=>$item['name'], 'id_category'=>$item['id_category']));
            $id_item = $this->db->get('id', 'items', array('name'=>$item['name'], 'id_category'=>$item['id_category']));
            // Store the values:
            $this->storeOptionValues($id_item, $item['options']);
        }
        
        // Update an item
        public function updateItem($id_item, $item)
        {
            $this->db->update('items', array('name'=>$item['name'], 'id_category'=>$item['id_category']), array('id'=>$id_item));
            // Store the values:
            $this->storeOptionValues($id_item, $item['options']);
        }
        
        // Store the values attached to this item:
        private function storeOptionValues($id_item, $optionValues)
        {
			$this->db->delete('values', array('id_item'=>$id_item));
            foreach($optionValues as $option) {
				$this->db->insert('values', array('id_item'=>$id_item, 'id_option'=>$option['id'], 'value'=>$option['value']));
			}
        }
        
        // Delete an item
        public function deleteItem($id_item)
        {
			$this->db->delete('items', array('id'=>$id_item));
			$this->db->delete('values', array('id_item'=>$id_item));
        }
        
        // Get all the records
        public function getRecords($id_category)
        {
            $records = array();
            $result  = $this->db->select('*', 'items', array('where'=>array('id_category'=>$id_category), 'orderby'=>'name'));
            while($record = mysql_fetch_assoc($result)) {
                array_push($records, $record);
            }
            return $records;
        }
    }
?>